import 'dart:io';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:management/utils/log_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///显示访客身份证照片
class VisitorPhoto extends StatelessWidget {
  final double width;
  final double height;
  final String filePath;
  final picker = ImagePicker();
  final VoidCallback? onTap;
  final BoxFit fit;

  VisitorPhoto(this.width, this.height,
      {required this.filePath,  this.onTap, this.fit = BoxFit.cover});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        width: this.width,
        height: this.height,
        padding: EdgeInsets.all(5),
        decoration: BoxDecoration(border: Border.all(), color: Colors.white),
        alignment: Alignment.center,
        child: TextUtil.isEmpty(filePath)
            ? Image.asset(
                "assets/images/portrait_capture_normal.png",
                fit: this.fit,
              )
            : Image.file(
                File(filePath),
                fit: this.fit,
                width: this.width * 0.5,
                height: this.height * 0.6,
              ),
      ),
      onTap: () {
        if (onTap != null) {
          onTap!();
        }
      },
    );
  }

// Future getImage() async {
//   final pickedFile = await picker.getImage(source: ImageSource.camera);
//
//   setState(() {
//     if (pickedFile != null) {
//       MyLog.d("照片路径：${pickedFile.path}");
//       _image = File(pickedFile.path);
//     } else {
//       MyLog.d("没有选中照片");
//     }
//   });
// }

}

@Deprecated("动画以后再考虑")
class CardReaderAnimation extends StatefulWidget {
  final Function onClick;

  CardReaderAnimation({required this.onClick});

  @override
  _CardReaderAnimationState createState() => _CardReaderAnimationState();
}

class _CardReaderAnimationState extends State<CardReaderAnimation>
    with TickerProviderStateMixin {
  double angle = 0.0;
  late AnimationController controller1;
  late AnimationController controller2;
  late Animation<double> animation1;
  late Animation<double> animation2;

  @override
  void initState() {
    super.initState();
    controller1 =
        AnimationController(duration: Duration(milliseconds: 150), vsync: this)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              controller1.reverse();
            }
            if (status == AnimationStatus.dismissed) {
              controller2.forward();
            }
          });
    controller2 =
        AnimationController(duration: Duration(milliseconds: 150), vsync: this)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              controller2.reverse();
            }
          });

    animation1 = Tween<double>(begin: 0.0, end: 0.30)
        .animate(CurvedAnimation(parent: controller1, curve: Curves.ease))
          ..addListener(() {
            setState(() {
              angle = animation1.value;
            });
          });
    animation2 = Tween<double>(begin: 0.0, end: -0.30)
        .animate(CurvedAnimation(parent: controller2, curve: Curves.ease))
          ..addListener(() {
            setState(() {
              angle = animation2.value;
            });
          });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Transform.rotate(
        angle: angle,
        child: Image.asset('assets/images/card_reader.png',
            width: 45.w, height: 45.w),
      ),
      onTap: () {
        MyLog.d("---读卡器---");
        controller1.forward();
        widget.onClick();
      },
    );
  }

  @override
  void dispose() {
    controller1.dispose();
    controller2.dispose();
    super.dispose();
  }
}
